<?php
/**
 * Eren Framework
 * 
 * Template Operate Class
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_View
{
    
    /**
     * Instance of this
     * 
     * @var Eren_View
     */
    private static $_instance = NULL;
    
    /**
     * The Datas assigned to de tpl
     * 
     * @var array()
     */
    private $_params = array();
    
    /**
     * Tpl List
     * 
     * @var array()
     */
    private $_files = array();
    
    /**
     * Realize Singleton Model
     */
    private function __construct(){}
    
    /**
     * Get Instance
     * 
     * @return this
     */
    public static function getInstance()
    {
        if(self::$_instance == NULL){
            self::$_instance = new Eren_View;
        }
        return self::$_instance;
    }
    
    /**
     * Render data to the view tpl
     * 
     * @param string $key
     * @param int|string|array $value
     */
    public function setData($key, $value)
    {
        if(is_array($key) || empty($key)){
            throw new Eren_Exception('The first param of setData cant be array or null');
        }
        $this->_params[$key] = $value;
    }
    
    /**
     * Set the path of view tpl
     * 
     * @param string $file
     */
    public function setView($file)
    {
        if(is_array($file) || empty($file)){
            throw new Eren_Exception('The param of setView cant be array or null');
        }
        $this->_files[]  = $file;
    }
    
    /**
     * Create and display the HTML
     * 
     * @param string $modle
     */
    public function display()
    {
        if($this->_files == NULL){
            return false;
        }
        if($this->_params){
            extract($this->_params);
        }
        $viewPath = PROJECT_PATH . 'Application/View/';
        foreach ($this->_files as $file){
            $viewFile = $viewPath . $file . '.php';
            if(file_exists($viewFile)){
                include($viewFile);
            }else{
                throw new Eren_Exception("Template '{$viewFile}' is not exist");
            }
        }
        return TRUE;
    }
}